<?php
namespace Admin\Controller;
use Org\Util\Rbac;
use Think\Controller;
class IndexController extends Controller {

    /**
     * 后台首页
     */
    public function index()
    {
        $this->assign('view_title','后台管理');
        $this->display();
    }

    public function viewmsg(){
        $userTable=M('wish');
        $list=$userTable->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function look(){
        echo "欢迎来到后台";
    }

    public function delete(){
        $this->success('此功能正在开发中...');
    }

    public function _initialize(){
        if(!isset($_SESSION[C('USER_AUTH_KEY')])){
            $this->error('请登录',U('Login/index'));
        }

        if(C('USER_AUTH_ON')){
            var_dump(Rbac::AccessDecision());
        }
    }

    /////////////////////////

    /*
     * RBAC
     * */

    //////////////////////////

    //用户列表
    public function user(){
        $this->user=$res=D('UserRelation')->field('password',true)->relation(true)->select();
        $this->display();
    }

    //角色列表
    public function role(){
        $roleTable=M('role');
        $list=$roleTable->select();
        $this->assign('list',$list);
        $this->display();
    }

    //节点列表
    public function node(){
        $field=array('id','name','title','pid');

        $nodeTable=M('node');
        $list=$nodeTable->order('sort')->field($field)->select();
        /*dump($list);*/
        $list=node_merge($list);
        /*dump($list);*/
        $this->assign('list',$list);
        $this->display();
    }

    //添加用户
    public function addUser(){
        if(IS_POST){

            if(I('post.password')){
                $pwd=I('post.password','','md5');
            }else{
                $pwd="";
            }

            $user=array(
                'username'=>I('username'),
                'password'=>$pwd,
                'logintime'=>time(),
                'loginip'=>get_client_ip(),
            );
            /*dump($user);exit;*/
            $userTable=D('user');
            $role=array();
            if($userTable->create($user)){
                if(implode($_POST['role_id'])) {
                    $uid=$userTable->add();
                    foreach ($_POST['role_id'] as $v) {
                        if (!empty($v)) {
                            $role[] = array(
                                'role_id' => $v,
                                'user_id' => $uid
                            );
                        }
                    }
                    M('role_user')->addAll($role);
                    $this->success('添加成功', U('Index/user'));
                }else{
                    $this->error('请为用户选择角色!');
                }
            }else{
                $this->error($userTable->getError());
            }
        }else{
            $this->role=M('role')->select();
            $this->display();
        }
    }

    //添加角色
    public function addRole(){
        if(!IS_POST) {
            $this->display();
        }elseif(IS_POST){
            /*dump($d=I('post.'));exit;*/
            $data=array(
                'name'=>I('post.name'),
                'remark'=>I('post.remark'),
                'status'=>I('post.status')
            );
            $roleTable=D('role');
            /*dump($roleTable->create($data));*/
            if($roleTable->create($data)){
                $roleTable->add();
                $this->success('创建角色成功!',U('Index/addRole'));
            }else{
                $this->error($roleTable->getError());
            }
        }
    }

    //添加节点
    public function addNode(){
        /*用level来判断节点是应用,控制器还是方法*/
        $level=I('level',1,'intval');
        $pid=I('pid',0,'intval');

        switch($level){
            case 1:
                $type='应用';
                break;
            case 2:
                $type='控制器';
                break;
            case 3:
                $type='方法';
                break;
        }
        $data=array(
            'type'=>$type,
            'level'=>$level,
            'pid'=>$pid
        );
        dump($data);
        $this->assign('data',$data);
        $this->display();


    }

    public function addNodeHandle(){
        $data=array(
            'name'=>I('post.name'),
            'title'=>I('post.title'),
            'status'=>I('post.status'),
            'sort'=>I('post.sort'),
            'pid'=>I('post.pid'),
            'level'=>I('post.level')
        );

        /*dump(I('post.'));exit;*/

        $nodeTable=D('node');
        if($nodeTable->create($data)){
            $nodeTable->add();
            $this->success('添加节点成功.正在跳转节点列表',U('Index/addNode'));
        }else{
            $this->error($nodeTable->getError());
        }
    }

    public function access(){
        $rid=I('rid',0,'intval');

        /*$list=M('node')->order('sort')->select();*/
        /*$this->list=node_merge($list);*/

        $field=array('id','name','title','pid');
        $node=M('node')->order('sort')->field($field)->select();

        //读取原有权限
        $access=M('access')->where(array('role_id'=>$rid))->getField('node_id',true);

        $this->list=$node=node_merge($node,$access);
        /*dump($node);dump($access);exit;*/

        $this->rid=$rid;
        $this->display();
    }

    public function setAccess(){
        $rid=I('post.rid',0,'intval');
        $accessTable=M('access');

        $accessTable->where(array('role_id'=>$rid))->delete();

        $data=array();

        foreach($_POST['access'] as $v){
            $tmp=explode('_',$v);
            $data[]=array(
                'role_id'=>$rid,
                'node_id'=>$tmp[0],
                'level'=>$tmp[1]
            );
        }

        if($accessTable->addAll($data)){
            $this->success('修改成功',U('Index/role'));
        }else{
            $this->error('修改失败');
        }
    }
}